package com.lagou.client;

import java.util.List;

/**
 * 提供现增加、删除、修改、查找以及遍历所有学生信息的功能
 */

public class AdditionsDeletionsModifications {

    //List<Student> list =new ArrayList();

    // 增加学生信息
    public static void stu_add(int id, String name, int age,List<Student> list) throws Exception {
        Student newStudent = new Student(id, name, age);
        boolean flag = list.contains(newStudent); //contains:判断是否包含参数指定的所有对象
        if (flag) {
            System.out.printf("学号%d已经存在，添加失败\n", id);

        } else {
            list.add(newStudent);
            System.out.printf("%s添加成功\n", name);
        }
    }

    // 删除学生信息
    public void deletions(List<Student> list, int id) {
        for (Student ts : list) {
            int index = list.indexOf(ts); // indexOf:查找参数指定的对象,返回索引(该学生对象在集合的索引)
            if (id == ts.getId()) {  //和输入的id相同
                // 删除
                list.remove(index);
                System.out.printf("%d删除成功\n", ts.getId());
            }

        }

    }

    // 修改学生信息
    public void modifications(int id, String name, int age, Student student) throws Exception {
        student.setId(id);
        student.setAge(age);
        student.setName(name);
        System.out.println("修改成功!");

    }

    // 查找学生信息
    public static Student Numbering(int id,List<Student> list) {
        for (Student st: list){
            if (id == st.getId()){
                return st;
            }
        }

        return null;
    }
    // 遍历学生信息

    public void Find(List<Student> list) {

        for (Student ts: list) {
            System.out.println(ts);
        }

    }





}

